﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RADB;
using System.Collections;
using System.Web.Script.Serialization;

namespace WaiterService
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.ContentType = "application/x-javascript";
            string username = Request["username"];
            string password = Request["password"];
            string logedin = Request["logedin"];

            JavaScriptSerializer serializer = (JavaScriptSerializer)Application["serializer"];

            Hashtable result = new Hashtable();
            result.Add("Success", true);

            if (logedin == "yes")
            {
                Response.Output.Write("Logged in");
                return;
            }

            if (username == null || password == null)
            {
                result["Success"] = false;
                result.Add("Error", "Invalid username / password");
                Response.Output.Write("({0})", serializer.Serialize(result));
                return;
            }

            IDataTransfer DTO = (IDataTransfer)Application["DTO"];
            int staffID = DTO.login(username, password);

            if (staffID == 0)
            {
                result["Success"] = false;
                result.Add("Error", "Wrong username / password");
                Response.Output.Write("({0})", serializer.Serialize(result));
                return;
            }
            else
            {
                result.Add("isManager", DTO.isManager(staffID));
                result.Add("isWaiter", DTO.isWaiter(staffID));
                result.Add("isHost", DTO.isHost(staffID));
                result.Add("isBusboy", DTO.isBusBoy(staffID));
                result.Add("isCook", DTO.isCook(staffID));

                result["Success"] = true;
                result.Add("WaiterID", staffID);
                Response.Output.Write("({0})", serializer.Serialize(result));
            }
        }
    }
}